Thread: Why am I using single quotes around "|"?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    Why am I using single quotes around "|"?

    Hi

    I myself wrote the code below some months ago. But now I have forgotten much of C++ I learned so please help me to rejuvenate the memory. Why did I use single quotes around "|"? Shouldn't I have used double quotes? I know there is a reason for using single quotes but I can't recall it. Please help me. Thank you.

    Code:
    /* arranging the three numbers in ascending order assuming the numbers 
    are distinct */
    
    // there would be six permutations in total
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
     	float A, B, C;
     	
     	cout << "enter the numbers A, B, C" << endl ;
     	
     	cout << "enter A = " ;
     	cin >> A;
     	
     	cout << "enter B = " ;
     	cin >> B;
     	
     	cout << "enter C = " ;
     	cin >> C;
     	
     	
            if ( (A < B) && (B < C) )
                cout << "the ascending order is " << A << '|' << B << '|' << C << endl; 
     	   
            else if ( (A < C) && (C < B) )
                cout << "the ascending order is " << A << '|' << C << '|' << B << endl; 
     	   
            else if ( (B < A) && (A < C) )
                cout << "the ascending order is " << B << '|' << A << '|' << C << endl; 
     	   
            else if ( (B < C) && (C < A) )
                cout << "the ascending order is " << B << '|' << C << '|' << A << endl; 
     	   
            else if ( (C < A) && (A < B) )
                cout << "the ascending order is " << C << '|' << A << '|' << B << endl; 
     	   
            else if ( (C < B) && (B < A) )
                cout << "the ascending order is " << C << '|' << B << '|' << A 
                << endl; 
        
            else
                cout << "Input error" << endl;
    		
    	
     	   
        system("pause");
        
        return 0;
        
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    'A' is a character that can be hardcoded into C++ , while "A" is a string literal which has an address, thus being more time consuming to fetch. And of course, the ostream could implement the output operator with single character as a simple put(), while the string equivalent would be something more complex.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    The real question you should be asking yourself is, why aren't you using the STL? Keeping the C mentality of things while trying to learn C++ can be dangerous and result in very bad habits. One possible C++ solution for your program is:
    Code:
    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    
    int main(void){
    
    	double number;
    	std::vector<double>mynumbers; //"array" of numbers
    	
    	std::cout<<"Enter numbers(EOF to quit):";
    	while(std::cin >> number){
    		mynumbers.push_back(number);//add numbers to our vector
    	}
    	
    	std::sort(mynumbers.begin(),mynumbers.end()); //sort numbers using STL
    
    	/*output numbers to screen*/
    	std::cout<<"Numbers in ascending order: ";
    	std::copy(mynumbers.begin(), //output numbers in vector using STL iterators
    		mynumbers.end(),
    		std::ostream_iterator<double>(std::cout,"|"));
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by GReaper View Post
    'A' is a character that can be hardcoded into C++ , while "A" is a string literal which has an address, thus being more time consuming to fetch. And of course, the ostream could implement the output operator with single character as a simple put(), while the string equivalent would be something more complex.
    The difference is so negligable that it isn't worth optimizing unless you can prove it's a bottleneck. Remember that premature optimization is evil™.
    Also, in this case, it really doesn't matter if you use single quotes or double quotes.
    A single quote represents a single character--which is exactly what '|' is--so it works. Nevertheless, "|" isn't wrong either, since it's a string with one character.
    Last edited by Elysia; 09-18-2011 at 04:49 PM. Reason: Spelling error
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, all of you.

    I think when I wrote that code I was simply proving to myself that I could also use '|' instead of "|" to the same effect. I usually do this just to teach myself.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  2. Printf a string using "string_name" (quotes)
    By g_p in forum C Programming
    Replies: 4
    Last Post: 04-25-2007, 10:41 AM
  3. outputing quotes(') and double quotes(")
    By lostboy in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-26-2002, 06:17 PM